package com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.review.subview

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import com.helasasa.maxfunds.lineofcredit.cashloan.R
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.http.hitPointCameroon

interface IReviewCameroonOldUserDialogListener {
    fun onClick(star: Int)
}

class ReviewCameroonOldUserDialog(var onItemClick: IReviewCameroonOldUserDialogListener) :
    DialogFragment() {

    private lateinit var detail: TextView
    private lateinit var star1: ImageView
    private lateinit var star2: ImageView
    private lateinit var star3: ImageView
    private lateinit var star4: ImageView
    private lateinit var star5: ImageView
    private lateinit var rate: TextView
    private lateinit var notNow: TextView
    private lateinit var close: ImageView

    private var starLevel = 0
    private var content: String? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        return inflater.inflate(R.layout.view_cameroon_review_old_user_dialog, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)

        detail = view.findViewById(R.id.content)
        star1 = view.findViewById(R.id.star1)
        star2 = view.findViewById(R.id.star2)
        star3 = view.findViewById(R.id.star3)
        star4 = view.findViewById(R.id.star4)
        star5 = view.findViewById(R.id.star5)
        rate = view.findViewById(R.id.rate)
        notNow = view.findViewById(R.id.notNow)
        close = view.findViewById(R.id.close)

        initializeView()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }

        if (content.isNullOrBlank()) {
            return
        }
        detail.text = content
    }

    private fun initializeView() {

        star1.setOnClickListener {
            starLevel = 1

            star1.setImageResource(R.drawable.pic_review_star_hl)
            star2.setImageResource(R.drawable.pic_review_star_nor)
            star3.setImageResource(R.drawable.pic_review_star_nor)
            star4.setImageResource(R.drawable.pic_review_star_nor)
            star5.setImageResource(R.drawable.pic_review_star_nor)
            star1.context.hitPointCameroon("REVIEW_NORMAL_DIALOG_STAR", starLevel.toString())

        }

        star2.setOnClickListener {
            starLevel = 2

            star1.setImageResource(R.drawable.pic_review_star_hl)
            star2.setImageResource(R.drawable.pic_review_star_hl)
            star3.setImageResource(R.drawable.pic_review_star_nor)
            star4.setImageResource(R.drawable.pic_review_star_nor)
            star5.setImageResource(R.drawable.pic_review_star_nor)
            star1.context.hitPointCameroon("REVIEW_NORMAL_DIALOG_STAR", starLevel.toString())

        }

        star3.setOnClickListener {
            starLevel = 3

            star1.setImageResource(R.drawable.pic_review_star_hl)
            star2.setImageResource(R.drawable.pic_review_star_hl)
            star3.setImageResource(R.drawable.pic_review_star_hl)
            star4.setImageResource(R.drawable.pic_review_star_nor)
            star5.setImageResource(R.drawable.pic_review_star_nor)
            star1.context.hitPointCameroon("REVIEW_NORMAL_DIALOG_STAR", starLevel.toString())

        }

        star4.setOnClickListener {
            starLevel = 4

            star1.setImageResource(R.drawable.pic_review_star_hl)
            star2.setImageResource(R.drawable.pic_review_star_hl)
            star3.setImageResource(R.drawable.pic_review_star_hl)
            star4.setImageResource(R.drawable.pic_review_star_hl)
            star5.setImageResource(R.drawable.pic_review_star_nor)
            star1.context.hitPointCameroon("REVIEW_NORMAL_DIALOG_STAR", starLevel.toString())

        }

        star5.setOnClickListener {
            starLevel = 5

            star1.setImageResource(R.drawable.pic_review_star_hl)
            star2.setImageResource(R.drawable.pic_review_star_hl)
            star3.setImageResource(R.drawable.pic_review_star_hl)
            star4.setImageResource(R.drawable.pic_review_star_hl)
            star5.setImageResource(R.drawable.pic_review_star_hl)
            star1.context.hitPointCameroon("REVIEW_NORMAL_DIALOG_STAR", starLevel.toString())

        }

        rate.setOnClickListener {
            star1.context.hitPointCameroon("REVIEW_NORMAL_DIALOG_RATE", starLevel.toString())

            val curType = starLevel
            onItemClick.onClick(curType)
            starLevel = 0
            dismiss()
        }

        notNow.setOnClickListener {
            starLevel = 0

            star1.setImageResource(R.drawable.pic_review_star_nor)
            star2.setImageResource(R.drawable.pic_review_star_nor)
            star3.setImageResource(R.drawable.pic_review_star_nor)
            star4.setImageResource(R.drawable.pic_review_star_nor)
            star5.setImageResource(R.drawable.pic_review_star_nor)
            star1.context.hitPointCameroon("REVIEW_NORMAL_DIALOG_NOT_NOW", starLevel.toString())

            dismiss()
        }

        close.setOnClickListener {
            starLevel = 0

            star1.setImageResource(R.drawable.pic_review_star_nor)
            star2.setImageResource(R.drawable.pic_review_star_nor)
            star3.setImageResource(R.drawable.pic_review_star_nor)
            star4.setImageResource(R.drawable.pic_review_star_nor)
            star5.setImageResource(R.drawable.pic_review_star_nor)
            star1.context.hitPointCameroon("REVIEW_NORMAL_DIALOG_CLOSE", starLevel.toString())

            dismiss()
        }
    }

    fun setDialogContent(content: String?) {
        this.content = content
    }
}